// pages/login/login.js
let app = getApp()

Page({

  /**
   * 页面的初始数据 
   */
  data: {
    // loading: false,
    password: '', 
    phoneNo: ''
  },
  onLoad: function(){
  },
  onReady: function() {
    wx.request({
      url: app.serverUrl + '/getSessionId',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function(res) {
        wx.setStorageSync('sessionId', 'JSESSIONID=' + res.data)

      }
    })
  },
  formSubmit: function(e) {
    let {
      phoneNo,
      password
    } = e.detail.value;
    if (phoneNo == '' || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(phoneNo)) {
      wx.showToast({
        title: '请正确填写手机号',
        icon: 'none',
        duration: 2000,
      })
    } else if (password = '') {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none',
        duration: 2000,
      })
    } else {
      console.log('就是你'+phoneNo);
      console.log(e.detail.value.password)
      wx.request({
        // console.log(phoneNo);
        // console.log(password)

        url: app.serverUrl + '/login',
        method: 'post',
        data: {
          userid: phoneNo,
          password: e.detail.value.password
        },
        success: (res) => {
          console.log(res.data)
          if (res.data.status == 200) {
            // 登录成功跳转 
            wx.showToast({
              title: '登录成功',
              icon: 'none',
              duration: 1500
            })
            setTimeout(function() {
              wx.hideToast()
            }, 2000)
            wx.reLaunch({
              url: '/pages/user/user'
            })
            // console.log(phoneNo);
            // console.log(password)
            //缓存
            wx.setStorage({
              key: 'userid',
              data: phoneNo,
            })
            wx.setStorageSync('userid', phoneNo)
            let resData = res.data;
            if (resData.success == true) {

              // app.globalData.userInfo = resData.result;
              // wx.setStorage({
              //   key: "userid",
              //   data: resData.result.userid
              // });
            }
          } else if (res.data.status == 500) {
            // 失败弹出框
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 3000
            })
          }
        }
      })
    }
  }
})